{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "43942fa9-9397-4a78-bdc9-637bf8a3ce8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_to_tree(data):\n",
    "    tree = []\n",
    "    mapping = {}\n",
    "\n",
    "    # 第一步：将每个节点存入映射表，初始化 children\n",
    "    for item in data:\n",
    "        item_id = item[\"id\"]\n",
    "        mapping[item_id] = {**item, \"children\": []}\n",
    "\n",
    "    # 第二步：根据 parentId 将节点插入到对应的父节点中\n",
    "    for item in data:\n",
    "        item_id = item[\"id\"]\n",
    "        parent_id = item[\"parentId\"]\n",
    "        node = mapping[item_id]\n",
    "\n",
    "        if parent_id == 0:\n",
    "            tree.append(node)\n",
    "        else:\n",
    "            if parent_id in mapping:\n",
    "                mapping[parent_id][\"children\"].append(node)\n",
    "            else:\n",
    "                # 可选：处理父节点不存在的情况\n",
    "                tree.append(node)\n",
    "\n",
    "    return tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4a72b42f-e0bf-4237-b74e-abbbb23c659a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'id': 1,\n",
      "  'name': '节点1',\n",
      "  'parentId': 0,\n",
      "  'children': [{'id': 3,\n",
      "                'name': '节点1-1',\n",
      "                'parentId': 1,\n",
      "                'children': [{'id': 6,\n",
      "                              'name': '节点1-1-1',\n",
      "                              'parentId': 3,\n",
      "                              'children': []}]},\n",
      "               {'id': 4, 'name': '节点1-2', 'parentId': 1, 'children': []}]},\n",
      " {'id': 2,\n",
      "  'name': '节点2',\n",
      "  'parentId': 0,\n",
      "  'children': [{'id': 5, 'name': '节点2-1', 'parentId': 2, 'children': []}]}]\n"
     ]
    }
   ],
   "source": [
    "data = [\n",
    "    {\"id\": 1, \"name\": \"节点1\", \"parentId\": 0},\n",
    "    {\"id\": 2, \"name\": \"节点2\", \"parentId\": 0},\n",
    "    {\"id\": 3, \"name\": \"节点1-1\", \"parentId\": 1},\n",
    "    {\"id\": 4, \"name\": \"节点1-2\", \"parentId\": 1},\n",
    "    {\"id\": 5, \"name\": \"节点2-1\", \"parentId\": 2},\n",
    "    {\"id\": 6, \"name\": \"节点1-1-1\", \"parentId\": 3},\n",
    "]\n",
    "\n",
    "tree = list_to_tree(data)\n",
    "import pprint\n",
    "\n",
    "pprint.pprint(tree, sort_dicts=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3b70de68-b8d6-43a2-86a4-888d5cb3fe5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'id': 1, 'name': '节点1', 'parentId': 0, 'children': [{'id': 3, 'name': '节点1-1', 'parentId': 1, 'children': [{'id': 6, 'name': '节点1-1-1', 'parentId': 3, 'children': []}]}, {'id': 4, 'name': '节点1-2', 'parentId': 1, 'children': []}]}, {'id': 2, 'name': '节点2', 'parentId': 0, 'children': [{'id': 5, 'name': '节点2-1', 'parentId': 2, 'children': []}]}]\n"
     ]
    }
   ],
   "source": [
    "print(tree)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
